﻿package com.shuqi.service;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.text.TextUtils;
import android.util.Log;

import com.shuqi.common.Config;

public class ConnectionChangeReceiver extends BroadcastReceiver {
    private final static String logTag = "ConnectionChangeReceiver";

    public static final int NET_TYPE_NO = 0; // 无网
    public static final int NET_TYPE_WIFI = 1; // wifi
    public static final int NET_TYPE_NO_WIFI = 2; // 非wifi

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.e(logTag, "网络状态改变了，改变之前：" + Config.getNetType(context));
        ConnectionChangeReceiver.getNetType(context);
        if(!TextUtils.isEmpty(Config.getNetType(context))){
            onConnectionChageActiveToDoSomething(context);
        }
    }
    /**网络改变的时候，做一些事情。注意：广播很短暂，不能做耗时操作。**/
    private void onConnectionChageActiveToDoSomething(Context context){
        Log.e(logTag, "网络连接上：启动检查书籍更新服务..");
        Intent intent = new Intent(context, CheckMarksUpdateService.class);
        context.startService(intent);
        Intent intent1 = new Intent(context, CheckPushMessageService.class);
        context.startService(intent1);
        
    }

    public static int getNetType(Context context) {
        int type = NET_TYPE_NO;
        try {
            // 判断当前网络类型
            ConnectivityManager manager =
                    (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo netWorkInfo = manager.getActiveNetworkInfo();
            if (netWorkInfo == null || !netWorkInfo.isAvailable()) {
                Config.setNetType(null);
                type = NET_TYPE_NO;
            } else if (netWorkInfo.getTypeName().equalsIgnoreCase("MOBILE")
                    && netWorkInfo.getExtraInfo().equalsIgnoreCase("cmwap")) {
                Config.setNetType("cmwap");
                type = NET_TYPE_NO_WIFI;
            } else if (netWorkInfo.getTypeName().equalsIgnoreCase("MOBILE")
                    && netWorkInfo.getExtraInfo().equalsIgnoreCase("cmnet")) {
                Config.setNetType("cmnet");
                type = NET_TYPE_NO_WIFI;
            } else if (netWorkInfo.getTypeName().equalsIgnoreCase("MOBILE")
                    && netWorkInfo.getExtraInfo().equalsIgnoreCase("3gwap")) {
                Config.setNetType("3gwap");
                type = NET_TYPE_NO_WIFI;
            } else if (netWorkInfo.getTypeName().equalsIgnoreCase("MOBILE")
                    && netWorkInfo.getExtraInfo().equalsIgnoreCase("3gnet")) {
                Config.setNetType("3gnet");
                type = NET_TYPE_NO_WIFI;
            } else if (netWorkInfo.getTypeName().equalsIgnoreCase("MOBILE")
                    && netWorkInfo.getExtraInfo().equalsIgnoreCase("uniwap")) {
                Config.setNetType("uniwap");
                type = NET_TYPE_NO_WIFI;
            } else if (netWorkInfo.getTypeName().equalsIgnoreCase("MOBILE")
                    && netWorkInfo.getExtraInfo().equalsIgnoreCase("uninet")) {
                Config.setNetType("uninet");
                type = NET_TYPE_NO_WIFI;
            } else if (netWorkInfo.getTypeName().equalsIgnoreCase("MOBILE")
                    && netWorkInfo.getExtraInfo().equalsIgnoreCase("#777")) {
                Config.setNetType("#777");
                type = NET_TYPE_NO_WIFI;
            } else if (netWorkInfo.getTypeName().equalsIgnoreCase("WIFI")) {
                Config.setNetType("wifi");
                type = NET_TYPE_WIFI;
            } else {
                if (netWorkInfo.getExtraInfo() != null) {
                    Config.setNetType(netWorkInfo.getExtraInfo());
                } else {
                    Config.setNetType("other");
                }
                type = NET_TYPE_NO_WIFI;
            }
            Log.e(logTag, "网络状态改变之后：" + Config.getNetType(context));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return type;
    }
}
